package com.bsit.obu.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bsit.obu.R;
import com.bsit.obu.base.BaseActivity;
import com.bsit.obu.dialog.PromptDialog;
import com.bsit.obu.utils.SPUtils;
import com.bsit.obu.utils.ToastUtils;


import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设置界面
 */
public class SettingActivity extends BaseActivity {


    @BindView(R.id.ip_show_tv)
    protected TextView mIpText;

    @BindView(R.id.port_show_tv)
    protected TextView mPortText;

    @BindView(R.id.obu_power_show_tv)
    protected TextView mObuPowerText;

    @BindView(R.id.more_iv)
    ImageView mMoreIv;              //菜单栏多图标

    @Override
    protected void initView() {
        setContentView(R.layout.activity_setting);

    }

    @Override
    protected void fillView() {
        super.fillView();
        String ip = (String) SPUtils.get(this,"ip","");
        String port = (String) SPUtils.get(this,"port","");
        if(!TextUtils.isEmpty(ip)){
            mIpText.setText(ip);
        }
        if(!TextUtils.isEmpty(port)){
            mPortText.setText(port);
        }

        titleTv.setText("IP和端口设置");
        mMoreIv.setVisibility(View.INVISIBLE);
    }
    PromptDialog dialog;

    @OnClick(R.id.set_ip_ly)
    void setIply(){
        dialog = new PromptDialog(this, "ip地址", mIpText.getText().toString(), new PromptDialog.onSubmitListener() {
            @Override
            public void onSubmit(String text) {
                mIpText.setText(text);
                dialog.dismiss();
                dialog = null;
            }

            @Override
            public void onCancle() {
                dialog.dismiss();
                dialog = null;
            }
        },-1, false);
        dialog.show();
    }

    @OnClick(R.id.set_port_ly)
    void setPortly(){
        dialog = new PromptDialog(this,"端口", mPortText.getText().toString(), new PromptDialog.onSubmitListener() {
            @Override
            public void onSubmit(String text) {
                mPortText.setText(text);
                dialog.dismiss();
                dialog = null;
            }

            @Override
            public void onCancle() {
                dialog.dismiss();
                dialog = null;
            }
        },-1, true);
        dialog.show();
    }

    @OnClick(R.id.set_obu_power)
    void setmObuPower(){
        AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this,AlertDialog.THEME_HOLO_LIGHT);
        //builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle("请选择要设置的功率");
        //    指定下拉列表的显示数据
        final String[] obu_power = {"远", "中", "近"};
        //    设置一个下拉的列表选择项
        builder.setItems(obu_power, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                //Toast.makeText(getActivity(), "选择的城市为：" + cities[which], Toast.LENGTH_SHORT).show();
                mObuPowerText.setText(obu_power[which]);
                SPUtils.put(SettingActivity.this,"prower",obu_power[which]);

            }
        });
        builder.show();
    }



    @OnClick(R.id.commit_btn)
    void commitSetting(){
        if(TextUtils.isEmpty(mIpText.getText().toString())) {
            ToastUtils.showToast(this, "ip不能为空");
            return;
        }
        if(TextUtils.isEmpty(mPortText.getText().toString())) {
            ToastUtils.showToast(this, "端口不能为空");
            return;
        }
        String ip = mIpText.getText().toString().trim();
        String port = mPortText.getText().toString().trim();
        SPUtils.put(this,"ip",ip);
        SPUtils.put(this,"port",port);
        ToastUtils.showToast(this, "设置成功");
    }
}
